package edu.cqie.operation.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.cqie.common.anno.Authentication;
import edu.cqie.common.domain.po.TestPO;
import edu.cqie.common.enums.AuthenticationType;
import edu.cqie.common.util.ApiResult;
import edu.cqie.common.util.FileTypeUtils;
import edu.cqie.common.util.MinioUtil;
import edu.cqie.common.util.PageUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/operation")
@RequiredArgsConstructor
public class TestController1 {

    private final RedisTemplate<String, Object> redisTemplate;

    private final MinioUtil minioUtil;

    @GetMapping("/test")
    public ApiResult test() {


        redisTemplate.opsForValue().set("keyObj", ApiResult.success());
        redisTemplate.opsForValue().set("keyString", "hello");

        TestPO testPO = new TestPO();
        testPO.setName("test1");
        testPO.setAge(18);
        redisTemplate.opsForValue().set("TestObj",testPO );
        Page<Object> page = PageUtil.getPage();
        long total = page.getTotal();

        ApiResult o = (ApiResult)redisTemplate.opsForValue().get("keyObj");
        TestPO o2 = (TestPO)redisTemplate.opsForValue().get("TestObj");

        System.out.println(o);
        System.out.println(o2);

        return ApiResult.success(o2);
    }

    @PostMapping("/minioTest")
    public ApiResult minioTest(@RequestBody MultipartFile file) {

        String bucketName = "group1";

        String fileName = "testUrl"+"/"+"testImg";

        String fileType = FileTypeUtils.getFileType(file);

        minioUtil.putObject( bucketName,  file , fileName , fileType);

        String returnStr = "/" + bucketName + "/" + fileName ;

        return ApiResult.success( Map.of("imgUrl",returnStr) );

    }

}
